package com.boot.auth.support.provider;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.boot.auth.support.LoginDTO;
import com.boot.framework.token.domain.LoginUser;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;

/**
 * @author yangfan
 * @date 2023/12/24
 * @Description
 */
@Component
public class AuthenticationFactoryProvider {


    public SaTokenInfo login(LoginDTO loginDTO) {
        AuthenticationProvider authenticationProvider = SpringUtil.getBean(loginDTO.getLoginType());
        LoginUser loginUser = authenticationProvider.verify(loginDTO);
        if (loginUser == null) {
            return null;
        } else {
            return createToken(loginUser);
        }
    }

    private SaTokenInfo createToken(LoginUser user) {
        StpUtil.login(user.getId());
        StpUtil.getSession().set("user", user);
        return StpUtil.getTokenInfo();
    }
    private LinkedHashMap<String,Object>buildLoginLogMap(LoginUser loginUser){
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put("browser",loginUser.getBrowser());//浏览器信息
        map.put("username",loginUser.getUsername());
        map.put("ipaddr",loginUser.getLoginIp());
        map.put("loginLocation","*** ***");
        map.put("os",loginUser.getOs());
        map.put("status","0");
        map.put("msg","登录成功");
        return map;
    }
}
